{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "#load \"..\\Extension\\LoadBaseTool.csx\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `get`和`=>`区别\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "public class Tool\n",
    "{\n",
    "    public bool state = false;\n",
    "    public void Connect() =>  state = true;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\r\n"
     ]
    }
   ],
   "source": [
    "public class Station \n",
    "{\n",
    "    public Tool _tool_01 { get; } = new Tool();\n",
    "    public Tool[] ToolsList => [_tool_01];\n",
    "    public void Connect() => ToolsList[0].Connect();\n",
    "}\n",
    "\n",
    "Station _station = new Station();\n",
    "_station.Connect();\n",
    "_station._tool_01.state.Dump();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\r\n"
     ]
    }
   ],
   "source": [
    "public class Station \n",
    "{\n",
    "    public Tool _tool_01 => new Tool();  // 注意这里的区别\n",
    "    public Tool[] ToolsList => [_tool_01];\n",
    "    public void Connect() => ToolsList[0].Connect();\n",
    "}\n",
    "\n",
    "Station _station = new Station();\n",
    "_station.Connect();\n",
    "_station._tool_01.state.Dump();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上述两个代码的区别在于 `=>` 和 `{get;}` 的区别，这两个实际上是不等价的。\n",
    "\n",
    "`=>` 每次都会创建一个新的 `Tool`，因此输出 `State` 的时候永远都是 `false`。而 `get` 会返回后台的字段。\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "polyglot-notebook"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "languageName": "csharp",
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
